package net.gd.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 获得各种各种形式的时间类型
 */
public class DateUtils {
	
	public static String getStringDateShort() {
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  String dateString = formatter.format(currentTime);
		  return dateString;
		 }
	
	public static Date parseFullDate(String dateTimeString) throws ParseException{
    	SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
		return dateFormat.parse(dateTimeString);   	
		
	}
	
	
	public static Date parseDate(String dateTimeString) throws ParseException{
		HashMap<String, String> dateRegFormat = new HashMap<String, String>();
	    dateRegFormat.put("^(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])$", "yyyy-MM-dd");
	    dateRegFormat.put("^(\\d{4})/(0\\d{1}|1[0-2])/(0\\d{1}|[12]\\d{1}|3[01])$", "yyyy/MM/dd");
	    dateRegFormat.put("^(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01]) (0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$", "yyyy-MM-dd HH:mm:ss");
	    dateRegFormat.put("^(\\d{4})/(0\\d{1}|1[0-2])/(0\\d{1}|[12]\\d{1}|3[01]) (0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$", "yyyy/MM/dd HH:mm:ss");
	    dateRegFormat.put("^(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])T(0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$", "yyyy-MM-dd'T'HH:mm:ss");
	    dateRegFormat.put("^(\\d{4})/(0\\d{1}|1[0-2])/(0\\d{1}|[12]\\d{1}|3[01])T(0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$", "yyyy/MM/dd'T'HH:mm:ss");
	    for (String key : dateRegFormat.keySet()) {
	        if (Pattern.compile(key).matcher(dateTimeString).matches()) {
	        	SimpleDateFormat dateFormat = new SimpleDateFormat(dateRegFormat.get(key));
	          
				return dateFormat.parse(dateTimeString);
		
	        }
	      }
	    return null;
	}

	private static Map dateFormats=new Hashtable();
	
	public static int dateDiff(Date d1, Date d2) {
		long n1 = d1.getTime();
		long n2 = d2.getTime();
		long diff = n1-n2;//Math.abs(n1 - n2);//绝对值
		
		diff /= 3600 * 1000 * 24;
		return (Long.valueOf(diff)).intValue();
	}
	
	
	private static DateFormat getDateFormat(String pattern){
		if(!dateFormats.containsKey(pattern)){
			DateFormat df = new SimpleDateFormat(pattern);
			dateFormats.put(pattern, df);
		}
		return (DateFormat)dateFormats.get(pattern); 
	}
	public static String formatDate(Date date,String pattern){
		DateFormat df = getDateFormat(pattern);
	    return df.format(date);
	}
	public static List getRangeDates(Date beginTime,Date endTime,int field, int amount){
		List list = new ArrayList();
		list.add(beginTime);
		Calendar cal = Calendar.getInstance();
		Date temp=beginTime;
		while(endTime.after(temp)){
			cal.setTime(temp);
			cal.add(field, amount);
			temp=cal.getTime();
			list.add(temp);
		}
		return list;
	}
	
	public static Date parseDate(String strDate,String pattern) {
		DateFormat df = getDateFormat(pattern);
		try {
			return df.parse(strDate);
		} catch (ParseException e) {
		}
		return null;
	}
	
	public static String getDispDate(String strDate,String srcPattern,String dstPattern) {
		DateFormat srcDf = getDateFormat(srcPattern);
		DateFormat dstDf = getDateFormat(dstPattern);
		try {
			return dstDf.format(srcDf.parse(strDate));
		} catch (Exception e) {
			System.out.println("---------strDate:"+strDate);
			System.out.println("---------srcPattern:"+srcPattern);
			System.out.println("---------dstPattern:"+dstPattern);
			
		}
		return "";
	}
	
    public static long getMilliseconds(String date,String pattern) {
		DateFormat df = getDateFormat(pattern);
		try {
			return df.parse(date).getTime();
		} catch (ParseException e) {
		}
		return 0L;
	}
    
    public static String getDispDate(Date date,String pattern) {
		DateFormat df = getDateFormat(pattern);
		return df.format(date);
	}
    
    public static String getDispNow(String pattern) {
    	return getDispDate(Calendar.getInstance().getTime(),pattern);
    }
    
    public static String getSysDate() {
    	return getDispNow("yyyyMMddHHmmss");
    }
    public static String getDisplayDate(String time){
    	return getDisplayDate(time,true);
    }
    public static String getDisplayDate(String time,boolean covertNull){
    	if(CheckUtils.isEmpty(time)){
    		if(covertNull){
    			return "---/--/--";
    		}
    		return "";
    	}
    	int length=time.length();
    	if(length==8){
    	}
    	if (time.length() == 8){
    		return getDispDate(time,"yyyyMMdd","yyyy-MM-dd");
		} else if (time.length() == 12){
    		return getDispDate(time,"yyyyMMddHHmm","yyyy-MM-dd");
		} else if (time.length() == 14){
    		return getDispDate(time,"yyyyMMddHHmmss","yyyy-MM-dd");
		} else{
	    	return getDispDate(time,"yyyyMMddHHmmss","yyyy-MM-dd");
		}
    	
    }
    public static String getDisplayYm(String time){
    	return getDisplayYm(time,true);
    }
    public static String getDisplayYm(String time,boolean covertNull){
    	if(CheckUtils.isEmpty(time)){
    		if(covertNull){
    			return "---/--";
    		}
    		return "";
    	}
    	return getDispDate(time,"yyyyMMddHHmmss","yyyy-MM");
    }
    public static String getDisplayTime(String time){
    	return getDisplayTime(time,true);
    }

    public static String getDisplayTime(String time,boolean covertNull){
    	if(CheckUtils.isEmpty(time)){
    		if(covertNull){
    			return "---/--/--";
    		}
    		return "";
    	}
    	
    	return getDispDate(time,"yyyyMMddHHmmss","yyyy-MM-dd HH:mm:ss");
    }
	public static long getTimeStamp(String time){
		return getTimeStamp(time,"yyyyMMddHHmmss");
	}
	public static long getTimeStamp(String time,String pattern){
		DateFormat format = null;
		if (time.length() == pattern.length())
		{
			format = getDateFormat(pattern);
		} else if (time.length() == 8)
		{
			format = getDateFormat("yyyyMMdd");
		} else if (time.length() == 12)
		{
			format = getDateFormat("yyyyMMddHHmm");
		} else if (time.length() == 14)
		{
			format = getDateFormat("yyyyMMddHHmmss");
		} else
		{
			format = getDateFormat("yyyyMMddHHmmss");
		}
	
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
	
		}
		return date.getTime();
	}
	
	public static String addTime(String time,int field, int amount,String pattern){
		Calendar cal = Calendar.getInstance();
		DateFormat df = getDateFormat(pattern);
		Date date = null;
		try {
			date = df.parse(time);
		} catch (ParseException e) {
	
		}
		cal.setTime(date);
		cal.add(field, amount);
		return df.format(cal.getTime());
	}
	
	public static String addTime(int field, int amount,String pattern){
		Calendar cal = Calendar.getInstance();
		cal.add(field, amount);
		return getDispDate(cal.getTime(),pattern);
	}
	
	public static String addTime(Date time,int field, int amount,String pattern){
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		cal.add(field, amount);
		return getDispDate(cal.getTime(),pattern);
	}
	
	public static Date getTime(String pattern, String time) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = format.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getTime(String pattern, Date date) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static String getTime(String pattern, Date date,Locale locale) {
		SimpleDateFormat format = new SimpleDateFormat(pattern,locale);
		return format.format(date);
	}
	
	
	public static String nextDate(String rq, int days) {
		if (rq == null || rq.equals("") || rq.length() < 8) {
			return "";
		}
		Calendar cal = Calendar.getInstance();
		Date time = getTime("yyyyMMdd", rq.substring(0, 8));
		cal.setTime(time);
		cal.add(Calendar.DATE, days);
		return getTime("yyyyMMdd", cal.getTime());
	}
	public static String getNowDate(){
    	return getDispNow("yyyy-MM-dd HH:mm:ss");
    }
	
	public static Date getFirstDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
		
		return c.getTime();
	}
	
	public static Date getNextMonthFirstDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);
		c.add(Calendar.MONTH, 1);
		return c.getTime();
	}
	
	static String pattern = "yyyy-MM-dd HH:mm:ss";
	
	public static String getDateTimeNow(){
		Date currentDate = new Date();
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(currentDate);
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(addTime("20120304",Calendar.DAY_OF_WEEK,10,"yyyyMMdd"));
		System.out.println(DateUtils.getSysDate().substring(0,8));
//		List list = getRangeDates(parseDate("201301","yyyyMM"),parseDate("201307","yyyyMM"),Calendar.MONTH,1);
//		
//		System.out.println(list);
		
		System.out.println(dateDiff(getTime("yyyyMMdd","20140530"),getTime("yyyyMMdd","20140601")));
	}
}
